import Vue from 'vue'
import Vuex from 'vuex'
import {httpPost} from '../api/main'
Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    defaultActive:sessionStorage.getItem("isactive") || '' ,//菜单高亮的值
    isCollapse:false, //菜单收起关闭
    badgeNum: 0,
    dialogInfo: false,
    tabslist:JSON.parse(sessionStorage.getItem("tabslist")) || [{
        menuid: 21,
        icon: 'el-icon-user-solid',
        menuname: '首页',
        url: "/home",
        alwaysShow:true
      }], //面包屑数据
      menuItem: JSON.parse(localStorage.getItem("menuItem")) || {}
  },
  getters: {
    badgeNum(state){
      let userid = sessionStorage.getItem("userid")
      // 获取购物车数量
      httpPost(`/fileSubscribe/shopping/cart/size?userid=${userid}`).then((res) => {
          if (res.code == 200) {
            state.badgeNum = res.data;
          } else {
              // this.$message.error(res.message);
          }
        }).catch((err) => {
            // this.$message.error(err.message);
        });
      return state.badgeNum
    },
    menuItem(state){
      return state.menuItem
    }
  },
  mutations: {
    setBadgeNum(state){
      let userid = sessionStorage.getItem("userid")
      // 获取购物车数量
      httpPost(`/fileSubscribe/shopping/cart/size?userid=${userid}`).then((res) => {
          if (res.code == 200) {
              state.badgeNum = res.data;
          } else {
              // this.$message.error(res.message);
          }
        }).catch((err) => {
            // this.$message.error(err.message);
        });
    },
    SetisCollapse(state){
       state.isCollapse =!state.isCollapse 
    },
    // setBadgeNum(state,value){
    //   state.badgeNum = value

    // },
    //修改面包屑数据
    SetMenu(state,value){
      //判断添加的数据是否存在
      if(value.menuname != '首页'){
        const index = state.tabslist.findIndex(item => item.menuname === value.menuname)
        //如果点击的菜单不存在 state.tabslist
        if(index === -1){
            state.tabslist.push(value)
            if (state.tabslist.length > 2){
              state.tabslist.splice(1,1)
            }
            console.log(state.tabslist);
            
            //增加完面包屑把数据存到本地做持久化储存
            sessionStorage.setItem("tabslist", JSON.stringify(state.tabslist))
        }
      }
      if(value.menuname == '首页'){
          //如果点击的首页 初始化面包屑数据
           state.tabslist = [value]
           //增加完面包屑把数据存到本地做持久化储存
           sessionStorage.setItem("tabslist", JSON.stringify(state.tabslist))
      }
    },
    // 购物车隐藏
    setDialogInfo( state, value){
      state.dialogInfo = value
    },
    //删除指定面包屑数据
    ClearTag(state,value){
      const index = state.tabslist.findIndex(item => item.menuname === value.menuname)
      state.tabslist.splice(index,1)
    },
    //修改菜单高亮的值
    SetDefaultActive(state,value){
      state.defaultActive = value
      sessionStorage.setItem('isactive', value);
    },
    setMenuItem(state,value){
      state.menuItem = value
      localStorage.setItem('menuItem', JSON.stringify(value));
    }
  },
  actions: {
  },
  modules: {
  }
})